﻿package fl.controls.listClasses
{
    import fl.containers.*;
    import flash.display.*;
    import flash.events.*;

    public class ImageCell extends CellRenderer implements ICellRenderer
    {
        protected var textOverlay:Shape;
        protected var loader:UILoader;
        private static var defaultStyles:Object = {imagePadding:1, textOverlayAlpha:0.7};

        public function ImageCell()
        {
            this.loader = new UILoader();
            this.loader.addEventListener(IOErrorEvent.IO_ERROR, this.handleErrorEvent, false, 0, true);
            this.loader.autoLoad = true;
            this.loader.scaleContent = true;
            addChild(this.loader);
            return;
        }// end function

        override public function get listData() : ListData
        {
            return _listData;
        }// end function

        override public function set listData(param1:ListData) : void
        {
            _listData = param1;
            label = _listData.label;
            var _loc_2:* = (_listData as TileListData).source;
            if (this.source != _loc_2)
            {
                this.source = _loc_2;
            }
            return;
        }// end function

        public function get source() : Object
        {
            return this.loader.source;
        }// end function

        public function set source(param1:Object) : void
        {
            this.loader.source = param1;
            return;
        }// end function

        override protected function configUI() : void
        {
            super.configUI();
            this.textOverlay = new Shape();
            var _loc_1:* = this.textOverlay.graphics;
            _loc_1.beginFill(16777215);
            _loc_1.drawRect(0, 0, 100, 100);
            _loc_1.endFill();
            return;
        }// end function

        override protected function draw() : void
        {
            super.draw();
            return;
        }// end function

        override protected function drawLayout() : void
        {
            var _loc_4:Number = NaN;
            var _loc_1:* = getStyleValue("imagePadding") as Number;
            this.loader.move(_loc_1, _loc_1);
            var _loc_2:* = width - _loc_1 * 2;
            var _loc_3:* = height - _loc_1 * 2;
            if (this.loader.width != _loc_2 && this.loader.height != _loc_3)
            {
                this.loader.setSize(_loc_2, _loc_3);
            }
            this.loader.drawNow();
            if (_label == "" || _label == null)
            {
                if (contains(textField))
                {
                    removeChild(textField);
                }
                if (contains(this.textOverlay))
                {
                    removeChild(this.textOverlay);
                }
            }
            else
            {
                _loc_4 = getStyleValue("textPadding") as Number;
                textField.width = Math.min(width - _loc_4 * 2, textField.textWidth + 5);
                textField.height = textField.textHeight + 5;
                textField.x = Math.max(_loc_4, width / 2 - textField.width / 2);
                textField.y = height - textField.height - _loc_4;
                this.textOverlay.x = _loc_1;
                this.textOverlay.height = textField.height + _loc_4 * 2;
                this.textOverlay.y = height - this.textOverlay.height - _loc_1;
                this.textOverlay.width = width - _loc_1 * 2;
                this.textOverlay.alpha = getStyleValue("textOverlayAlpha") as Number;
                addChild(this.textOverlay);
                addChild(textField);
            }
            background.width = width;
            background.height = height;
            return;
        }// end function

        protected function handleErrorEvent(event:IOErrorEvent) : void
        {
            dispatchEvent(event);
            return;
        }// end function

        public static function getStyleDefinition() : Object
        {
            return mergeStyles(defaultStyles, CellRenderer.getStyleDefinition());
        }// end function

    }
}
